package com.fhnew.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.BankList;
import com.fhnew.app.model.network.entity.req.ResgetMoney;
import com.fhnew.app.ui.activity.userinfo.ActivityData;
import com.fhnew.app.ui.dialogactivity.WithdrawDialog;
import com.fhnew.app.util.ActivityUtil;
import com.fhnew.app.util.OnItemClickListener;
import com.fhnew.app.util.SYSOUtils;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 提款
 */
public class WithdrawActivity extends AppCompatActivity {

    private ImageView reback;
    private TextView moneytext;
    private TextView cardmeneeger;
    private ImageView refrashmoney;
    private LinearLayout addbank;
    private RecyclerView banklist;

    private Adapter adapter;
    private GridLayoutManager layoutManager;
    private ArrayList<BankList.DataBean.ListBean> dataBean = new ArrayList<>();

    private void initView () {
        reback = findViewById(R.id.reback);
        moneytext = findViewById(R.id.moneytext);
        cardmeneeger = findViewById(R.id.cardmeneeger);
        refrashmoney = findViewById(R.id.refrashmoney);
        addbank = findViewById(R.id.addbank);
        banklist = findViewById(R.id.banklist);

        reback.setOnClickListener(onClickListener);
        refrashmoney.setOnClickListener(onClickListener);
        addbank.setOnClickListener(onClickListener);
        cardmeneeger.setOnClickListener(onClickListener);

        getUserBankList();
        layoutManager = new GridLayoutManager(this, 1, LinearLayoutManager.VERTICAL, false);
        banklist.setLayoutManager(layoutManager);
        adapter = new Adapter();
        banklist.setAdapter(adapter);
        moneytext.setText(UserModel.user.money);
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        ActivityUtil.fullScreen(this);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_withdraw);
        initView();
        initInfo();
    }

    @Override
    protected void onResume () {
        super.onResume();
        getUserBankList();
        moneytext.setText(UserModel.user.money);
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();
        userModel.unRegistListener(userInformationChanged);

    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.reback:
                    finish();
                    break;
                case R.id.refrashmoney:
                    getMoney();
                    break;
                case R.id.addbank:
                    Intent intent1 = new Intent(WithdrawActivity.this, ActivityData.class);
                    intent1.putExtra("intentm", "绑卡");
                    startActivity(intent1);
                    break;
                case R.id.cardmeneeger:
                    Intent intent = new Intent(WithdrawActivity.this, ActivityData.class);
                    intent.putExtra("intentm", "绑卡");
                    startActivity(intent);
                    break;
            }
        }
    };


    private void getMoney () {
        HttpJsonUtils.getHttpApiInstance()
                .getMoney(Constants.getuseAdress() + "api/m/user/getMoney", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResgetMoney>() {
                    @Override
                    protected void onCustomNext (ResgetMoney resActivity) {
                        if (resActivity.code == 0) {
                            UserModel.user.money = resActivity.data.balance;
                            moneytext.setText(UserModel.user.money);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }


    private void getisVerifyPhone () {
        HttpJsonUtils.getHttpApiInstance()
                .getUserBankList(Constants.getuseAdress() + "api/m/bank/isVerifyPhone", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<BankList>() {
                    @Override
                    protected void onCustomNext (BankList bankList) {

                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    private void getUserBankList () {
        HttpJsonUtils.getHttpApiInstance()
                .getUserBankList(Constants.getuseAdress() + "api/m/user/getUserBankList", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<BankList>() {
                    @Override
                    protected void onCustomNext (BankList bankList) {
                        if (bankList.code == 0 && bankList.data.list.size() > 0) {
                            addbank.setVisibility(View.GONE);
                            banklist.setVisibility(View.VISIBLE);
                            dataBean.clear();
                            dataBean.addAll(bankList.data.list);
                            adapter.notifyDataSetChanged();
                        } else {
                            addbank.setVisibility(View.VISIBLE);
                            banklist.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });

    }

    private UserModel.UserInformationChanged userInformationChanged = new UserModel.UserInformationChanged() {
        @Override
        public void userInformationChanged () {
            if(!userModel.isLogin()){
                finish();
            }
        }
    };

    //===========================================================
    //
    //===========================================================
    private UserModel userModel;

    private void initInfo () {
        userModel = UserModel.getInstance(this);
        userModel.registListener(userInformationChanged);
    }

    //===========================================================
    // Adapter
    //===========================================================

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter.MViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int i) {
            return new MViewHolder(LayoutInflater.from(WithdrawActivity.this)
                    .inflate(R.layout.withdrawitem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder (@NonNull Adapter.MViewHolder holder, int position) {
            try {
                holder.itembankname.setText(UserModel.user.fullName + "");
                holder.itembancode.setText(dataBean.get(position).cardNo);
                holder.itemcardname.setText(dataBean.get(position).bank_name);
                holder.itembt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        //取款
                        Intent intent = new Intent(WithdrawActivity.this, WithdrawDialog.class);
                        intent.putExtra("bid", dataBean.get(position).bid + "");
                        startActivity(intent);
                    }
                });
            } catch (Exception e) {
            }
        }

        @Override
        public int getItemCount () {
            return dataBean.size();
        }

        @Override
        public void onItemClick (View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            TextView itembankname, itembancode, itemcardname, itembt;

            public MViewHolder (@NonNull View itemView) {
                super(itemView);

                itembankname = itemView.findViewById(R.id.itembankname);
                itemcardname = itemView.findViewById(R.id.itemcardname);
                itembancode = itemView.findViewById(R.id.itembancode);
                itembt = itemView.findViewById(R.id.itembt);

            }
        }
    }

    //===========================================================
    //
    //===========================================================


    //    @OnClick({R.id.reback, R.id.menuebt, R.id.withdrawSubmit, R.id.Binding})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.reback:
//                finish();
//                break;
//            case R.id.menuebt:
//                Intent intentp = new Intent(WithdrawActivity.this, ActivityData.class);
//                intentp.putExtra("intentm", "清单");
//                startActivity(intentp);
//                finish();
//                break;
//            case R.id.withdrawSubmit:
////
////                if (TextUtils.isEmpty(Withdrawal.getText().toString().trim())) {
////                    Toast.makeText(WithdrawActivity.this, "请输入提款金额", Toast.LENGTH_LONG).show();
////                    return;
////                }
////                if (TextUtils.isEmpty(Withdrawalpass.getText().toString().trim())) {
////                    Toast.makeText(WithdrawActivity.this, "请输入提款密码", Toast.LENGTH_LONG).show();
////                    return;
////                }
//                withdrawSubmit();
//                break;
//            case R.id.Binding:
//                Intent intent = new Intent(WithdrawActivity.this, ActivityData.class);
//                intent.putExtra("intentm", "绑卡");
//                startActivity(intent);
//                break;
//        }
//    }

    private void withdrawSubmit () {
//        HttpJsonUtils.getHttpApiInstance()
//                .withdrawSubmit(Constants.getuseAdress() + "api/m/bank/withdrawSubmit.do",
//                        Withdrawal.getText().toString().trim(), Md5.toMd5(Withdrawalpass.getText().toString().trim()), UserModel.Token)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<update_PwdBean>(WithdrawActivity.this) {
//                    @Override
//                    protected void onCustomNext(update_PwdBean bankList) {
//                        if (bankList.getCode() == 0) {
//                            Toast.makeText(WithdrawActivity.this, "" + bankList.getMsg(), Toast.LENGTH_LONG).show();
//                            Withdrawalpass.setText("");
//                            Withdrawal.setText("");
//                        } else {
//                            Toast.makeText(WithdrawActivity.this, "" + bankList.getMsg(), Toast.LENGTH_LONG).show();
//                        }
//                    }
//
//                    @Override
//                    protected void onSubscribeCustom(Disposable disposable) {
//
//                    }
//
//                    @Override
//                    protected void onErrorCustom(Throwable e) {
//                        SYSOUtils.sOut(e.toString());
//                    }
//                });

    }


}
